﻿using GraphTast.Web.DbAdapters;
using GraphTast.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity;

namespace GraphTast.Web.Controllers
{
    [Authorize]
    public class SetController : ApiController
    {
        IGraphSetAdapter _adapter = new GraphSetAdapter();

        // GET api/set
        public IHttpActionResult Get()
        {
            List<SetCaptureVM> sets = _adapter.GetUserSetList(User.Identity.GetUserId());
            return Ok(sets);
        }

        // GET api/set/:id
        public IHttpActionResult Get(int id)
        {
            SetCaptureVM set = _adapter.GetSet(id, User.Identity.GetUserId());
            if (set != null)
            {
                return Ok(set);
            }
            return Unauthorized();
        }

        // POST api/set
        public IHttpActionResult Post([FromBody] SetCaptureVM vm)
        {
            int setId = _adapter.SaveGraphSet(vm, User.Identity.GetUserId());
            if (setId > 0)
            {
                return Ok(setId);
            }
            return Unauthorized();
        }

        // PUT api/set/:id
        public IHttpActionResult Put(int id, [FromBody] SetCaptureVM vm)
        {
            bool res = _adapter.DeleteGraphSet(id, User.Identity.GetUserId());
            int setId;
            if (res)
            {
                setId = _adapter.SaveGraphSet(vm, User.Identity.GetUserId());
                if (setId > 0)
                {
                    return Ok(setId);
                }
            }
            return Unauthorized();
        }

        // DELETE api/set
        public IHttpActionResult Delete(int id)
        {
            bool res = _adapter.DeleteGraphSet(id, User.Identity.GetUserId());
            if (res)
            {
                return Ok();
            }
            return Unauthorized();
        }

    }
}
